package com.example.lj.uav.adapter;

import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lj.uav.R;
import com.example.lj.uav.holder.MediaLibraryHolder;
import com.example.lj.uav.utils.Constants;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import dji.sdk.media.MediaFile;

public class MediaLibraryAdapter extends RecyclerView.Adapter<MediaLibraryHolder> {
    private List<MediaFile> dataList;
    private int height;
    private Handler mHandler;
    private String[] downloadList;
    public boolean isSelecting;
    public List<MediaFile> selectedFiles;

    public MediaLibraryAdapter(List<MediaFile> dataList,  Handler mHandler) {
        this.dataList = dataList;
//        this.height = height;
        this.mHandler = mHandler;
        File file = new File(Environment.getExternalStorageDirectory()+"/UAV/MEDIA");
        downloadList = file.list();
        selectedFiles = new ArrayList<>();
    }

    public void fillList(List<MediaFile> list){
        dataList.clear();
        dataList.addAll(list);
        notifyDataSetChanged();
    }

    public List<MediaFile> getDataList(){
        return dataList;
    }

    @Override
    public MediaLibraryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_media_library,null);
        return new MediaLibraryHolder(view);
    }

    @Override
    public void onBindViewHolder(final MediaLibraryHolder holder, final int position) {
        if (downloadList!=null&&downloadList.length>0){
            for (int i=0;i<downloadList.length;i++){
                if (downloadList[i].equals(dataList.get(position).getFileName())){
                    holder.iv_download.setVisibility(View.VISIBLE);
                    break;
                }
            }
        }
        holder.iv.setImageBitmap(dataList.get(position).getThumbnail());
        holder.tv_name.setText(dataList.get(position).getFileName());
        holder.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isSelecting){
                    if (!selectedFiles.contains(dataList.get(position))){
                        holder.iv_selected.setVisibility(View.VISIBLE);
                        selectedFiles.add(dataList.get(position));
                    }else{
                        holder.iv_selected.setVisibility(View.GONE);
                        selectedFiles.remove(dataList.get(position));
                    }
                }else{
                    Message msg = mHandler.obtainMessage();
                    msg.what = Constants.OPEN_MEDIA_FILE;
                    msg.arg1 = position;
                    mHandler.sendMessage(msg);
                }
            }
        });
        holder.rl.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (!isSelecting){
                    holder.iv_selected.setVisibility(View.VISIBLE);
                    isSelecting = true;
                    selectedFiles.add(dataList.get(position));
                    mHandler.sendEmptyMessage(Constants.START_SELECTING_FILES);
                }
                return false;
            }
        });
        //设置item高度
//        ViewGroup.LayoutParams layoutParams = holder.rl.getLayoutParams();
//        layoutParams.height = height;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public void removeSelectedItem() {
        if (selectedFiles.size()>0){
            Message msg = mHandler.obtainMessage();
            msg.what = Constants.START_DELETING_FILES;
            msg.obj = selectedFiles;
            mHandler.sendMessage(msg);
        }
    }
}
